using System;
using System.Web.UI.WebControls;
using TimeSheetBLL;

namespace TimeSheet
{
    /// <summary>
    /// Summary description for ChangePassword.
    /// </summary>
    public class ChangePassword : TimeSheetForm
    {
        protected TextBox txtPassword;
        protected TextBox txtVerify;
        protected RequiredFieldValidator RequiredFieldValidator1;
        protected RangeValidator RangeValidator1;
        protected Label lblMessage;
        protected Button btnChange;

        private void Page_Load(object sender, EventArgs e)
        {
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        private void btnChange_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                try
                {
                    if (txtPassword.Text.Equals(txtVerify.Text))
                    {
                        User user = GetUser();
                        user.Password = txtPassword.Text;
                        BrokerFactory.Save(user);
                    }
                    else
                    {
                        lblMessage.Text = "Your passwords do not match.";
                    }
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "There was an error updating your password.";
                }
                Response.Redirect("TimeSheet.aspx");
            }
        }
    }
}